Skip to content

feat(metrics): initialise MULTIPLEXED_METRIC_ROUTING_KEY for routing#19096

Open
harshit078 wants to merge 22 commits intogetsentry:developfrom
harshit078:routing-to-different-dsn
Open

feat(metrics): initialise MULTIPLEXED_METRIC_ROUTING_KEY for routing#19096
harshit078 wants to merge 22 commits intogetsentry:developfrom
harshit078:routing-to-different-dsn

Conversation

@harshit078
Copy link
Contributor

@harshit078 harshit078 commented Jan 29, 2026

Before submitting a pull request, please take a look at our
Contributing guidelines and verify:

  • If you've added code that should be tested, please add tests.
  • Ensure your code lints and the test suite passes (yarn lint) & (yarn test).
  • Link an issue if there is one related to your pull request. If no issue is linked, one will be auto-generated and linked.

Closes #18913

What changed

  • implemented attribute-based routing for metrics
  • added MetricRoutingInfo with dsn and release as its field
  • added routing to MetricOptions and enabled captureMetric to inject routing info into attributes
  • similarly added routing to InternalCaptureMetricOptions
  • created _stripRoutingAttributes function which removes routing info before sending to sentry

@harshit078 harshit078 marked this pull request as ready for review February 2, 2026 12:05
@harshit078
Copy link
Contributor Author

Hi @chargome, the PR is ready to review. Thanks !

@Lms24 Lms24 requested a review from chargome February 4, 2026 09:05
Copy link
Member

@chargome chargome left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, thanks for opening the PR! I left some comments. Also, we definitely want to test this behaviour somehow in a browser integration test.

const container = Array.isArray(item) ? (item[1] as SerializedMetricContainer) : undefined;
const containerItems = container?.items;
if (containerItems) {
metric = containerItems[0];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't this mean we always just pull the first metric?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes it would mean we pull the first metric. What I'm thinking now after your comment is that I can add a logic which will check if all metrics and route to same or multiple destinations. Whats your opinion ?

}

const transports = actualMatcher({ envelope, getEvent, getMetric })
.map(result => {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: A race condition occurs when sending metrics to multiple transports with different releases. The shared envelope is mutated concurrently, leading to unpredictable release values on metrics.
Severity: HIGH

Suggested Fix

To prevent the race condition, ensure that each transport operates on a unique copy of the envelope's items. Before applyReleaseToMetrics is called, deep-clone the envelope's items array (envelope[1]) so that mutations within one transport's send method do not affect others.

Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent.
Verify if this is a real issue. If it is, propose a fix; if not, explain why it's not
valid.

Location: packages/core/src/transports/multiplexed.ts#L146

Potential issue: A race condition exists when sending metrics to multiple transports
with different `release` versions. The `overrideDsn` function creates a new envelope
header for each transport but reuses the same reference to the envelope's items array.
When `makeOverrideReleaseTransport` calls `applyReleaseToMetrics`, it mutates this
shared array by replacing it with new metric objects containing the `release`. Because
multiple transports do this concurrently via `Promise.all`, the final `release` value on
the metrics is unpredictable, depending on which mutation finishes last. This leads to
data corruption where metrics can be assigned the wrong release version.

Copy link

@cursor cursor bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 2 potential issues.

Bugbot Autofix is OFF. To automatically fix reported issues with Cloud Agents, enable Autofix in the Cursor dashboard.

}
}
});
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Variable strippedCount incremented but never used

Low Severity

In stripRoutingAttributesFromMetrics, the variable strippedCount is declared and conditionally incremented via DEBUG_BUILD && strippedCount++ but is never used afterward. A reviewer mentioned adding a debug log here, suggesting this was intended for logging but was never completed. The variable serves no purpose without an accompanying log statement.

Fix in Cursor Fix in Web

export { createTransport } from './transports/base';
export { makeOfflineTransport } from './transports/offline';
export { makeMultiplexedTransport, MULTIPLEXED_TRANSPORT_EXTRA_KEY } from './transports/multiplexed';
export { makeMultiplexedTransport } from './transports/multiplexed';
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed public export MULTIPLEXED_TRANSPORT_EXTRA_KEY is breaking change

Medium Severity

The constant MULTIPLEXED_TRANSPORT_EXTRA_KEY was previously exported from @sentry/core and @sentry/browser but has been removed from the public exports. Existing consumers who import this constant will experience a breaking change. Per the review rules, removal of publicly exported constants requires proper deprecation notices.

Additional Locations (1)

Fix in Cursor Fix in Web

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support routing metrics to different DSNs in MFE setup

2 participants